<?php
/**
 * @Project: None
 * @FileName: Event.php
 * @FilePath: D:\thinkphp6\tp\app\controller\Event.php
 * @Author: zishuiliufeng
 * @Date:   2021-01-28 17:21:49
 * @Last Modified by:   zishuiliufeng
 * @Last Modified time: 2021-02-13 19:55:47
 */
namespace app\controller;
use think\Facade\Db;
use think\facade\Request;
use app\BaseController;
use app\Response;
use app\model\RoleAttribute as RAttr;
use app\model\GameAttribute as GAttr;
use app\model\RoleInfo;
use app\model\RoleEvent;
use app\controller\Role as RoleCon;
use app\common;

class Event extends BaseController{

    /**
     * [getList 获取角色事件列表]
     * @return [type] [description]
     */
    public function getList(){
        $param = $this->request->param();
        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;
        //默认获取未使用的事件列表,0未使用，1使用，当传大于1的值时表示获取全部信息
        $is_used = isset($param['is_used'])?(int)$param['is_used']:0;

        //获取角色名称与图片地址
        $role = RoleInfo::where('id',$role_id)->findOrEmpty();
        if($role->isEmpty()){
            Response::show(404,'角色不存在');
        }
        
        $http_img = common::getHttpImg($role->img);
        
        $role_info = array(
            'role_name'     => $role->name,
            'role_en_name'  => $role->en_name,
            'role_img'      => $role->img,
            'role_http_img' => $http_img
            );

        $where = [];
        if($is_used == 0 || $is_used == 1){
            $where[] = ['is_used','=',$is_used];
        }

        $event = RoleEvent::where('role_id',$role_id)->where($where)->order('id', 'asc')->select();

        $event_list = array();
        if(!$event->isEmpty()){
            foreach ($event as $k => $v) {
                $event_list[] = array(
                    'id'            => $v->id,
                    'role_id'       => $v->role_id,
                    'name'          => $v->name,
                    'en_name'       => $v->en_name,
                    'introduction'  => $v->introduction,
                    'trigger'       => $v->trigger,
                    'spend_time'    => $v->spend_time,
                    'is_used'       => $v->is_used,
                    'create_time'   => $v->create_time,
                    'update_time'   => $v->update_time
                    );
            }
        }
        $result = array(
            'role_info' => $role_info,
            'event_list' => $event_list
            );
        Response::show(200,'人物事件列表',$result);
    }

    /**
     * [getEventList 只获取人物事件列表]
     * @return [type] [description]
     */
    public function getEventList(){
        $param = $this->request->param();
        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;
        //默认获取未使用的事件列表,0未使用，1使用，当传大于1的值时表示获取全部信息
        $is_used = isset($param['is_used'])?(int)$param['is_used']:2;

        //当前人物事件id，当其传值时，需要获取该人物指定的人物事件信息
        $now_role_event_id = isset($param['now_role_event_id'])?(int)$param['now_role_event_id']:0;

        //获取角色名称与图片地址
        $role = RoleInfo::where('id',$role_id)->findOrEmpty();
        if($role->isEmpty()){
            Response::show(404,'角色不存在');
        }

        $map1 = [];
        if($is_used == 0 || $is_used == 1){
            $map1[] = ['is_used','=',$is_used];
        }

        $map2 = [];
        if($now_role_event_id > 0){
            $map1[] = ['role_id','=',$role_id];
            $map2[] = ['role_id','=',$role_id];
            $map2[] = ['id','=',$now_role_event_id];
        }

        if($now_role_event_id > 0){
            $event = RoleEvent::whereOr([$map1,$map2])->order('id', 'asc')->select();
        }else{
            $event = RoleEvent::where('role_id',$role_id)->where($map1)->order('id', 'asc')->select();
        }
        

        $event_list = array();
        if(!$event->isEmpty()){
            foreach ($event as $k => $v) {
                $event_list[] = array(
                    'id'            => $v->id,
                    'role_id'       => $v->role_id,
                    'name'          => $v->name,
                    'en_name'       => $v->en_name,
                    'is_used'       => $v->is_used
                    );
            }
        }
        Response::show(200,'人物事件列表',$event_list);
    }
    /**
     * [changeUsed 修改人物事件状态]
     * 当重新选择游戏事件时，新的事件状态置为1，旧的事件状态重置为0
     * @return [type] [description]
     */
    public function changeUsed()
    {   
        $param = $this->request->param();
        $id = isset($param['id'])?(int)$param['id']:0;
        //默认修改状态为已使用,0未使用,1已使用
        $is_used = isset($param['is_used'])?(int)$param['is_used']:1;

        $old_id = isset($param['old_id'])?(int)$param['old_id']:0;
        $old_used = isset($param['old_used'])?(int)$param['old_used']:0;

        //判断新的事件是否存在啊
        $event = RoleEvent::where('id',$id)->findOrEmpty();
        if(!$event->isEmpty()){
            //事件存在则修改使用状态
            $event->is_used = $is_used;
            $event->update_time = date('Y-m-d H:i:s');
            $event->save();
        }

        //如果有旧的事件
        if($old_id > 0){
            //重置旧的事件状态
            $old_event = RoleEvent::where('id',$old_id)->findOrEmpty();
            if(!$old_event->isEmpty()){
                //事件存在则修改使用状态
                $old_event->is_used = $old_used;
                $old_event->update_time = date('Y-m-d H:i:s');
                $old_event->save();
            }
        }

        Response::show(200,'修改成功');
    }
    /**
     * [addEvent 新增人物事件]
     */
    public function addEvent()
    {
        $param = $this->request->param();
        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;
        $name = isset($param['name'])?(string)$param['name']:'';
        $en_name = isset($param['en_name'])?(string)$param['en_name']:'';
        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';
        $trigger = isset($param['trigger'])?(string)$param['trigger']:'';
        $spend_time = isset($param['spend_time'])?(int)$param['spend_time']:0;

        //验证角色是否存在
        $role = RoleInfo::where('id',$role_id)->findOrEmpty();
        if($role->isEmpty()){
            Response::show(404,'角色不存在');
        }

        //新增事件
        $event = new RoleEvent;
        $event->role_id = $role_id;
        $event->name = $name;
        $event->en_name = $en_name;
        $event->introduction = $introduction;
        $event->trigger = $trigger;
        $event->spend_time = $spend_time;
        $event->is_used = 0;
        $event->create_time = date('Y-m-d H:i:s');
        $event->update_time = date('Y-m-d H:i:s');
        $event->save();
        //获取自增ID
        $id = $event->id;
        Response::show(200,'新增成功',$id);
    }
    /**
     * [editEvent 修改人物事件]
     * @return [type] [description]
     */
    public function editEvent()
    {
        $param = $this->request->param();
        $id = isset($param['id'])?(int)$param['id']:0;
        $name = isset($param['name'])?(string)$param['name']:'';
        $en_name = isset($param['en_name'])?(string)$param['en_name']:'';
        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';
        $trigger = isset($param['trigger'])?(string)$param['trigger']:'';
        $spend_time = isset($param['spend_time'])?(int)$param['spend_time']:0;
        //默认修改状态为未使用,0未使用,1已使用
        $is_used = isset($param['is_used'])?(int)$param['is_used']:0;

        //验证事件是否存在
        $event = RoleEvent::where('id',$id)->findOrEmpty();
        if($event->isEmpty()){
            Response::show(404,'事件不存在');
        }

        //存在则修改
        $event->name = $name;
        $event->en_name = $en_name;
        $event->introduction = $introduction;
        $event->trigger = $trigger;
        $event->spend_time = $spend_time;
        $event->is_used = $is_used;
        $event->update_time = date('Y-m-d H:i:s');
        $event->save();
        Response::show(200,'修改成功');
    }
    /**
     * [delEvent 删除事件]
     * @return [type] [description]
     */
    public function delEvent()
    {
        $param = $this->request->param();
        $id = isset($param['id'])?(int)$param['id']:0;
        //验证事件是否存在
        $event = RoleEvent::where('id',$id)->findOrEmpty();
        if($event->isEmpty()){
            Response::show(404,'事件不存在');
        }

        $del = $event->delete();    //bool事件，删除成功返回true,失败返回false
        if($del){
            $code = 200;
            $msg = '删除成功';
        }else{
            $code = 204;
            $msg = '删除失败';
        }

        Response::show($code,$msg);
    }
}